//
//  SafePhoneCodeController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2025/6/27.
//  Copyright © 2025 zhuchongjin. All rights reserved.
//

#import "SafePhoneCodeController.h"
#import "ForcePasswordModificationController.h"

@interface SafePhoneCodeController ()
@property (nonatomic, strong) NSString *codeStr;

@end

@implementation SafePhoneCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.codeButton.layer.masksToBounds = YES;
    self.codeButton.layer.cornerRadius = 5;
    self.commitButton.layer.masksToBounds = YES;
    self.commitButton.layer.cornerRadius = 10;
    self.bottomHeight.constant = BR_BOTTOM_MARGIN;
    self.topheight.constant = kNavBarHeight + 10;
    self.phoneTextField.keyboardType =   UIKeyboardTypeNumberPad;
    [self setTitleName:@"安全手机验证"];
    [self setBackLeftButton:@""];
    [self.codeButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
}
- (IBAction)codeButtonAction:(UIButton *)sender {
    [self getCodeAction];
    __block NSInteger time = 59;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);

    dispatch_source_set_event_handler(_timer, ^{

         if(time <= 0){

         dispatch_source_cancel(_timer);

         dispatch_async(dispatch_get_main_queue(), ^{

                [self.codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
             
             [self.codeButton setBackgroundColor:[UIColor colorWithRed:75/255.0 green:166/255.0 blue:157/255.0 alpha:1]];

                 self.codeButton.userInteractionEnabled = YES;

            });

        }else{

         int seconds = time % 60;

         dispatch_async(dispatch_get_main_queue(), ^{

                [self.codeButton setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];

             [self.codeButton setBackgroundColor:[UIColor colorWithRed:199/255.0 green:205/255.0 blue:213/255.0 alpha:1]];

        self.codeButton.userInteractionEnabled = NO;

            });

            time--;

        }

    });

    dispatch_resume(_timer);
}
- (IBAction)commitButtonAction:(UIButton *)sender {
    if (self.phoneTextField.text.length == 0) {
        [CJAlert showStringDismissWithDelay:@"请输入手机号"];
        return;
    }
    if (self.phoneTextField.text.length != 11) {
        [CJAlert showStringDismissWithDelay:@"手机号必须是11位"];
        return;
    }
    if (self.codeTextField.text.length == 0) {
        [CJAlert showStringDismissWithDelay:@"请输入验证码"];
        return;
    }
    if ([_isQChangePassword isEqualToString:@"1"]) {
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/party/safetyVerify",CJBaseUrl);
        NSDictionary *dicPra = @{
            @"phone":self.phoneTextField.text,
            @"verifyCode" : self.codeTextField.text
        };
        @weakify(self);
        [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strUrl parameters:dicPra status:ArpaDefault success:^(id  _Nonnull responseObject) {
            ForcePasswordModificationController *vc = [[ForcePasswordModificationController alloc] init];
            vc.phoneStr = self.phoneTextField.text;
            vc.codeStr = self.codeTextField.text;
            vc.isChangePassword = @"1";
            [self pushVc:vc];
            } failure:^(NSError * _Nonnull error) {
                
            }];
    }else{
        ForcePasswordModificationController *vc = [[ForcePasswordModificationController alloc] init];
        vc.phoneStr = self.phoneTextField.text;
        vc.codeStr = self.codeTextField.text;
        [self pushVc:vc];
    }

}

- (void)getCodeAction{
    
    if (self.phoneTextField.text.length == 0) {
        [CJAlert showStringDismissWithDelay:@"请输入手机号"];
        return;
    }
    if (self.phoneTextField.text.length != 11) {
        [CJAlert showStringDismissWithDelay:@"手机号必须是11位"];
        return;
    }
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tps-api/sms/sendSafetyVerificationCode",CJBaseUrl);
    NSString *code = [[NSUserDefaults standardUserDefaults] objectForKey:@"partyGroupCode"];
    NSDictionary *dicPra = @{
        @"phone":self.phoneTextField.text,
        @"partyGroupCode" : code
    };
    
    @weakify(self);
    [[HDNetworking sharedHDNetworking] POST:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
       
        
    } failure:^(NSError * _Nonnull error) {

    }];
}
@end
